package com.framework2.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.framework.utils.ScreenUtils;
import com.framework2.baseEvent.BaseOnClickListener;
import com.framework2.util.PicToastUtil;
import com.library.percent.PercentRelativeLayout;
import com.yaxin.yyt.R;

/**
 * 二维码扫描失败时弹出框
 *
 * @author YobertJomi
 * className InputPopupwindow
 * created at  2017/3/20  15:13
 */

public class InputPopupwindow extends PopupWindow {
    private Context context;
    private View viewRoot;
    private EditText et_input;
    private ImageView iv_delete;
    private ImageView iv_clear;
    private TextView tv_pop_title;
    private TextView tv_pop_content;
    private TextView tv_cancel;
    private TextView tv_sure;

    private boolean showClearButton;
    private OnSureClickListener onclickListener;
    private OnCancelClickListener onCancelClickListener;

    public InputPopupwindow(Context context) {
        super(context);
        this.context = context;
        initView();
        initStyle();
        initEvent();
    }

    private void initView() {
        viewRoot = LayoutInflater.from(context).inflate(
                R.layout.popwindow_input_info, null);
        iv_delete = (ImageView) viewRoot.findViewById(R.id.iv_delete);
        iv_clear = (ImageView) viewRoot.findViewById(R.id.iv_clear);
        tv_pop_title = (TextView) viewRoot.findViewById(R.id.tv_pop_title);
        tv_pop_content = (TextView) viewRoot.findViewById(R.id.tv_pop_content);
        et_input = (EditText) viewRoot.findViewById(R.id.et_input);
        tv_cancel = (TextView) viewRoot.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) viewRoot.findViewById(R.id.tv_sure);
//        setWidth(ScreenUtils.getInstance().getScreenWidthPx(context));
//        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
//        setContentView(viewRoot);

        setWidth(ScreenUtils.getInstance().getScreenWidthPx(context));
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        int height = (int) ((ScreenUtils.getInstance().getScreenHeightPx(context) - ScreenUtils.getInstance()
                .getStatusBarHeightPx(context) - ScreenUtils.getInstance().dip2px(context, 50)) * 0.34);
        PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(ScreenUtils
                .getInstance().getScreenWidthPx(context) - ScreenUtils.getInstance().dip2px(context, 40), height);
        viewRoot.findViewById(R.id.rootView).setLayoutParams(layoutParams);
        setContentView(viewRoot);
    }

    private void initStyle() {
        // 设置整个popupwindow的弹出，收起方式
        setAnimationStyle(R.style.popWindowAnim);
        // 需要设置一下此参数，点击外边可消失
        setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        setFocusable(true);
    }

    private void initEvent() {
        iv_delete.setOnClickListener(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                dismiss();
            }
        });
        tv_cancel.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                if (null != onCancelClickListener) {
                    onCancelClickListener.onCancelClickListener();
                }
            }
        });
        tv_sure.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                if (TextUtils.isEmpty(et_input.getText())) {
                    PicToastUtil.getInstance().showToast(context, "输入不能为空!");
                    return;
                }
                if (null != onclickListener) {
                    onclickListener.onSureClickListener(et_input.getText().toString());
                }
            }
        });
        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && showClearButton) {
                    iv_clear.setVisibility(View.VISIBLE);
                } else {
                    iv_clear.setVisibility(View.INVISIBLE);
                }
            }
        });
        iv_clear.setOnClickListener(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                et_input.setText("");
                iv_clear.setVisibility(View.INVISIBLE);
            }
        });
    }

    public EditText getEditText() {
        return et_input;
    }

    public InputPopupwindow setTitle(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_pop_title.setText(charSequence);
        return this;
    }

    public InputPopupwindow setContent(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_pop_content.setText(charSequence);
        return this;
    }

    public InputPopupwindow setContentVisible(boolean visible) {
        tv_pop_content.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public InputPopupwindow setSureButtonText(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_sure.setText(charSequence);
        return this;
    }

    public InputPopupwindow setEditTextHint(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            et_input.setHint(charSequence);
        return this;
    }

    public InputPopupwindow setShowClearButton(boolean showClearButton) {
        this.showClearButton = showClearButton;
        return this;
    }

    public InputPopupwindow showClearButton() {
        setShowClearButton(true);
        iv_clear.setVisibility(View.VISIBLE);
        return this;
    }

    public InputPopupwindow setOnSureClickListener(OnSureClickListener onclickListener) {
        this.onclickListener = onclickListener;
        return this;
    }

    public InputPopupwindow setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
        return this;
    }

    public interface OnSureClickListener {
        void onSureClickListener(String info);
    }

    public interface OnCancelClickListener {
        void onCancelClickListener();
    }
}
